package com.mini.cn.goods.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by fly on 17/5/21.
 */
public class TimeUtil {

    /**
     * 拿到指定的日期
     *
     * @param day
     * @return
     */
    public static Date getCurrDay(int day) {
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, day);
        Date endday = cal.getTime();
        return endday;
    }


    /**
     *
     * @param year
     * @return
     */
    public static Date getCurrYear(int year) {
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.MONTH,0);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        cal.add(Calendar.YEAR,year);
        Date endyear = cal.getTime();
        return endyear;
    }
    /**
     * 拿到指定时间后面的时间
     *
     * @param hm
     * @return
     */
    public static String getAfterHM(Date hm, int min) {
        long hmtime = hm.getTime();
        hmtime += min * 60 * 1000;
        Date da = new Date(hmtime);
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm");
        return dateFormat.format(da);
    }

    /**
     * @param hm
     * @param min
     * @return
     */
    public static Date getAfterHMDate(Date hm, int min) {
        try {
            long hmtime = hm.getTime();
            hmtime += min * 60 * 1000;
            Date da = new Date(hmtime);
            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm");
            Date time = dateFormat.parse(dateFormat.format(da));
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 转化时间格式
     *
     * @param time
     * @return
     */
    public static String coverFormatTimeByMin(String time) {
        DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.valueOf(time));
        String format = formatter.format(calendar.getTime());
        return format;
    }

    /**
     * 把date转成字符串
     *
     * @param date
     * @return
     */
    public static String coverFormatTime(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time = simpleDateFormat.format(date);
        return time;
    }

    /**
     * 把string转成date
     *
     * @param time
     * @return
     */
    public static Date coverFormatToDate(String time) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = simpleDateFormat.parse(time);
        return date;

    }

    /**
     * 把string转成date
     *
     * @param time
     * @return
     */
    public static Date coverFormatToDateNoHm(String time) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = simpleDateFormat.parse(time);
        return date;

    }

    /**
     * 设置工作时间
     *
     * @param time
     * @param day
     * @return
     */
    public static Date creatWorkTime(int time, Date day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(day);
        cal.set(Calendar.HOUR_OF_DAY, time);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();

    }

    /**
     * @param date
     * @return
     */
    public static String coverFormatByDate(Date date) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(date);
            return dateString;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 生成前几天的date
     *
     * @param number
     * @return
     */
    public static Date creatAddDay(int number, Date targetDate) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(targetDate);
        calendar.add(Calendar.DAY_OF_MONTH, number);//把日期往后增加一天.整数往后推,负数往前移动
        targetDate = calendar.getTime();   //这个时间就是日期往后推一天的结果
        return targetDate;
    }


    /**
     * 把当前时间转化成想要的格式
     *
     * @return
     */
    public static Date getCurrTimeHm() {
        try {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = simpleDateFormat.parse(simpleDateFormat.format(date));
            return time;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *
     * @param time
     * @return
     */
    public static Date coverSecondToDate(long time) {
        Date date = new Date(time);
        return date;
    }

    /**
     * 判断时间是不是今天
     * @param date
     * @return    是返回true，不是返回false
     */
    public static boolean isNow(long date) {
        long currday=getCurrDay(0).getTime();
        return date==currday;
    }


    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate
     *            较小的时间
     * @param bdate
     *            较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate)
            throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 格式HH:mm 比较分钟和小时的时间大小
     * @param d1
     * @param d2
     * @return
     */
    public static long compareTimeByHM(String d1,String d2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            long result = sdf.parse(d1).getTime() - sdf.parse(d2).getTime();
            return result;
        }catch (Exception e){
            return 0;
        }

    }

    public static void main(String args[]) {

        System.out.println(getCurrYear(1));
    }
}
